package xcmg.device.web.announcement;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.announcement.BasicCommentService;
import xcmg.device.service.vo.announcement.BasicCommentVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("/basicComment")
public class BasicCommentController extends BasicController {

    @Autowired
    private BasicCommentService basicCommentService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("评论信息查询")
    public ActionResult list(@RequestBody SearchCommonVO<BasicCommentVO> condition)throws Exception{
        BasicCommentVO basicCommentVO = condition.getFilters();
        if(basicCommentVO == null){
            basicCommentVO = new BasicCommentVO();
        }
        //根据ANNOUNCEMENT_ID查询所有评论信息
        condition.setFilters(basicCommentVO);
        PageCommonVO pageCommonVO = basicCommentService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("评论信息添加")
    public ActionResult addAnnouncement(@RequestBody BasicCommentVO basicCommentVO)throws Exception{
        if(null == basicCommentVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        basicCommentVO.setOrgId(getOrgId());
        basicCommentVO.setOrgName(getDeptName());
        basicCommentVO.setCreateId(getUserId());
        basicCommentVO.setCreateName(getUserName());
        basicCommentVO.setCreateDate(new Date());
        basicCommentVO.setUpdateId(getUserId());
        basicCommentVO.setUpdateDate(new Date());
        try {
            return actionResult(basicCommentService.add(basicCommentVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.AnnouncementManage.Announcement_EDIT)
    @ApiOperation(value = "更新评论信息")
    public ActionResult update(@RequestBody BasicCommentVO model)throws Exception{
        //更新参数是否存在
        if(model==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        model.setUpdateId(getUserId());
        model.setOrgId(getOrgId());
        model.setOrgName(getDeptName());
        model.setUpdateDate(new Date());
        ErrorCode ret=basicCommentService.update(model);
        return actionResult(ret);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "删除评论信息")
    public ActionResult remove(@RequestBody BasicCommentVO model){
        String[] ids = model.getIds();
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=basicCommentService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "评论信息单个查询")
    public ActionResult item(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        BasicCommentVO result=basicCommentService.item(id);
        return actionValueResult(result);
    }
}
